package et.six.eta;

import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Handler;
import android.support.annotation.NonNull;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.NestedScrollView;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import et.six.eta.Adapter.ListAdapter;
import et.six.eta.Adapter.SyAdapter;
import et.six.eta.Ban.FoodList;
import et.six.eta.View.DatailActivity;
import et.six.eta.View.LoginActivity;
import et.six.eta.View.Sideslip.developer_activity;
import et.six.eta.View.Sideslip.linear_message_view;
import et.six.eta.View.Sideslip.purchase_record;
import et.six.eta.View.Sideslip.set_up_butt;
import et.six.eta.View.Dialog;
import et.six.eta.View.SignInActivity;
import et.six.eta.View.zhujiemian_fenlei;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener,SwipeRefreshLayout.OnRefreshListener{
    //SHA1 F7:57:63:1A:F7:B8:92:9C:CC:FD:9C:D1:43:3E:1D:92:94:6D:CE:84
    //http://lbsyun.baidu.com/index.php?title=android-locsdk/geosdk-android-download
    ViewPager vp;
    LinearLayout ll;
    NestedScrollView scrollView;
    int imgs[] = {R.mipmap.a1, R.mipmap.a2, R.mipmap.a3, R.mipmap.a4};
    ImageView[] imageView = new ImageView[imgs.length];
    Handler h = new Handler();
    ListView lv;
    String message = "";
    String food_Name;
    String food_Price;
    String food_Img;
    String food_Detail;
    String food_Id;
    TextView drawer_bt;
    LinearLayout ll_item1, ll_item2, ll_item3, ll_item4, ll_item5, ll_item6, ll_item7, ll_item8;
    //上边李旭





    TextView tv_name;//姓名
    TextView tv_integral;//积分
    TextView tv_balance;//余额

    TextView linear_message;//我的订单
    TextView linear_purchase_record;//购买记录
    TextView linear_food_search;//美食搜寻
    TextView linear_the_development_team;//开发团队
    TextView linear_set_up;
    LinearLayout ll_drawer;
    private int wDrawer;
    DrawerLayout dl_drawer;
    private LinearLayout ll_main_page;
    private SwipeRefreshLayout refreshLayout;//刷新控件
    String tv_pwd;
    Button butt_sign_out;



    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vp = findViewById(R.id.vp);
        ll = findViewById(R.id.ll);
        lv = findViewById(R.id.lv);
        scrollView = findViewById(R.id.scroll);
        lv.setOnItemClickListener(this);

        ll_item1 = findViewById(R.id.ll_item1);
        ll_item2 = findViewById(R.id.ll_item2);
        ll_item3 = findViewById(R.id.ll_item3);
        ll_item4 = findViewById(R.id.ll_item4);
        ll_item5 = findViewById(R.id.ll_item5);
        ll_item6 = findViewById(R.id.ll_item6);
        ll_item7 = findViewById(R.id.ll_item7);
        ll_item8 = findViewById(R.id.ll_item8);
        drawer_bt = findViewById(R.id.drawer_bt);
        ll_drawer = findViewById(R.id.ll_drawer);
        ll_item1.setOnClickListener(oc);
        ll_item2.setOnClickListener(oc);
        ll_item3.setOnClickListener(oc);
        ll_item4.setOnClickListener(oc);
        ll_item5.setOnClickListener(oc);
        ll_item6.setOnClickListener(oc);
        ll_item7.setOnClickListener(oc);
        ll_item8.setOnClickListener(oc);




        SyAdapter s = new SyAdapter(imgs);
        vp.setAdapter(s);
        initDot();
        int middle = s.getCount() / 2;
        int start = middle - middle % 4;
        selct();
        vp.setCurrentItem(start, false);
        vp.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        h.removeCallbacks(r);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        h.postDelayed(r, 2000);
                        break;
                }
                return false;
            }
        });
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changdot(position % imgs.length);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //onCreate 重这开始写

        tv_name = findViewById(R.id.tv_name);
        tv_integral = findViewById(R.id.tv_integral);
        tv_balance = findViewById(R.id.tv_balance);
        butt_sign_out=findViewById(R.id.butt_sign_out);

        linear_message = findViewById(R.id.linear_message);//我的订单
        linear_purchase_record = findViewById(R.id.linear_purchase_record);//购买记录
        linear_food_search = findViewById(R.id.linear_food_search);//美食搜寻
        linear_the_development_team = findViewById(R.id.linear_the_development_team);//开发团队
        linear_set_up = findViewById(R.id.linear_set_up);//设置


        ll_main_page = findViewById(R.id.ll_main_page);
        dl_drawer = findViewById(R.id.dl_drawer);

        butt_sign_out.setOnClickListener(onclick);
        linear_message.setOnClickListener(onclick);//我的订单
        linear_purchase_record.setOnClickListener(onclick);//购买记录
        linear_food_search.setOnClickListener(onclick);//美食搜寻
        linear_the_development_team.setOnClickListener(onclick);//开发团队
        linear_set_up.setOnClickListener(onclick);//设置

        if(LoginActivity.static_name==null){
            tv_name.setText(SignInActivity.static_name);
        }else {
            tv_name.setText(LoginActivity.static_name);
        }





ll_drawer.post(new Runnable() {
    @Override
    public void run() {
        wDrawer=ll_drawer.getMeasuredWidth();
        ObjectAnimator anim= ObjectAnimator.ofFloat(linear_message,"alpha",1f,0F);
        ObjectAnimator anim1= ObjectAnimator.ofFloat(linear_purchase_record,"alpha",1,0F);
        ObjectAnimator anim2= ObjectAnimator.ofFloat(linear_food_search,"alpha",1,0F);
        ObjectAnimator anim3= ObjectAnimator.ofFloat(linear_the_development_team,"alpha",1,0F);
        ObjectAnimator anim4= ObjectAnimator.ofFloat(linear_set_up,"alpha",1,0F);
        anim.start();
        anim1.start();
        anim2.start();
        anim3.start();
        anim4.start();
    }
});
        ll_drawer.post(new Runnable() {
            @Override
            public void run() {
                wDrawer = ll_drawer.getMeasuredWidth();


                ObjectAnimator anim = ObjectAnimator.ofFloat(linear_message, "alpha", 1f, 0F);
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(linear_purchase_record, "alpha", 1, 0F);
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(linear_food_search, "alpha", 1, 0F);
                ObjectAnimator anim3 = ObjectAnimator.ofFloat(linear_the_development_team, "alpha", 1, 0F);
                ObjectAnimator anim4 = ObjectAnimator.ofFloat(linear_set_up, "alpha", 1, 0F);
                anim.start();
                anim1.start();
                anim2.start();
                anim3.start();
                anim4.start();
            }


        });
        dl_drawer.addDrawerListener(new DrawerLayout.DrawerListener() {
            @Override
            public void onDrawerSlide(@NonNull View drawerView, float slideOffset) {
                ll_drawer.setTranslationX(wDrawer / 2 - wDrawer / 2 * slideOffset);
                ll_main_page.setTranslationX(wDrawer * slideOffset);

    }

            @Override
            public void onDrawerOpened(@NonNull View drawerView) {
                ObjectAnimator anim = ObjectAnimator.ofFloat(linear_message, "alpha", 0, 1F);
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(linear_purchase_record, "alpha", 0, 1F);
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(linear_food_search, "alpha", 0, 1F);
                ObjectAnimator anim3 = ObjectAnimator.ofFloat(linear_the_development_team, "alpha", 0, 1F);
                ObjectAnimator anim4 = ObjectAnimator.ofFloat(linear_set_up, "alpha", 0, 1F);
                anim.setDuration(1000);
                anim1.setDuration(2000);
                anim2.setDuration(3000);
                anim3.setDuration(4000);
                anim4.setDuration(5000);
                anim.start();
                anim1.start();
                anim2.start();
                anim3.start();
                anim4.start();


            }

            @Override
            public void onDrawerClosed(@NonNull View drawerView) {
                ObjectAnimator anim = ObjectAnimator.ofFloat(linear_message, "alpha", 1f, 0F);
                ObjectAnimator anim1 = ObjectAnimator.ofFloat(linear_purchase_record, "alpha", 1, 0F);
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(linear_food_search, "alpha", 1, 0F);
                ObjectAnimator anim3 = ObjectAnimator.ofFloat(linear_the_development_team, "alpha", 1, 0F);
                ObjectAnimator anim4 = ObjectAnimator.ofFloat(linear_set_up, "alpha", 1, 0F);
                anim.start();
                anim1.start();
                anim2.start();
                anim3.start();
                anim4.start();


            }

    @Override
    public void onDrawerStateChanged(int newState) {
        switch (newState){
            case DrawerLayout.STATE_IDLE: // drag拖拽 idle闲置 settling沉淀
                break;
        }

            }
        });

//        Intent it=getIntent();
//        String tv_names=it.getStringExtra("name");
                    //刷新图标的属性
        refreshLayout=findViewById(R.id.refreshlayout);
        refreshLayout.setColorSchemeResources(android.R.color.holo_blue_light, android.R.color.holo_orange_light, android.R
                .color.holo_green_light);
        // 设置刷新时圆形图标的大小。只可传递2个参数，SwipeRefreshLayout.LARGE或SwipeRefreshLayout.DEFAULT。
        refreshLayout.setSize(SwipeRefreshLayout.LARGE);
        // 设置刷新时圆形图标的背景色。
        refreshLayout.setProgressBackgroundColorSchemeResource(android.R.color.background_light);
        // 设置刷新时的圆形图标。scale：是否缩放；start：圆形图标在刷新前的起始位置；end：圆形图标的偏移量。
        refreshLayout.setProgressViewOffset(true, 100, 200);
        // 设置会触发下拉刷新的手势滑动距离
        refreshLayout.setDistanceToTriggerSync(100);
        // 设置刷新的监听事件
        refreshLayout.setOnRefreshListener(this);
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                refreshLayout.setEnabled(scrollView.getScrollY()==0);
            }
        });
        scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
            @Override
            public void onScrollChanged() {
                refreshLayout.setEnabled(scrollView.getScrollY()==0);
            }
        });

    }
    /*-----------------------------onCreate--------------------------------*/












    View.OnClickListener onclick = new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            switch (v.getId()) {
                case R.id.linear_message:
                    Intent lit = new Intent(MainActivity.this, linear_message_view.class);
                    lit.putExtra("name",LoginActivity.static_name);
                    lit.putExtra("pwd",LoginActivity.static_pwd);
                    startActivity(lit);
                    finish();
                    break;

                case R.id.linear_purchase_record:
                    Intent record=new Intent(MainActivity.this,purchase_record.class);
                    record.putExtra("name",LoginActivity.static_name);
                    startActivity(record);

                    break;

                case R.id.linear_food_search:
                    Intent iter=new Intent(MainActivity.this,zhujiemian_fenlei.class);
                    iter.putExtra("acc", "水果");
                    startActivity(iter);
                    break;

                case R.id.linear_the_development_team:
                    Intent it = new Intent(MainActivity.this, developer_activity.class);
                    startActivity(it);
                    finish();
                    break;
                case R.id.linear_set_up:

                    Intent set_up = new Intent(MainActivity.this, set_up_butt.class);
                    startActivity(set_up);
                    finish();
                    break;
                case R.id.butt_sign_out:
                    Intent sign = new Intent(MainActivity.this, LoginActivity.class);
                    startActivity(sign);
                    finish();
                    break;


            }

        }

    };





    private void initDot() {
        for (int i = 0; i < imgs.length; i++) {
            imageView[i] = new ImageView(this);
            imageView[i].setImageResource(R.drawable.selector_dot);
            imageView[i].setPadding(10, 10, 10, 10);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(30, 30);
            ll.addView(imageView[i], params);
        }
        changdot(0);
    }

    public void changdot(int position) {
        for (int i = 0; i < imageView.length; i++) {
            imageView[i].setEnabled(i == position);
        }
    }

    @Override
    protected void onStart() {
        h.postDelayed(r, 3000);
        super.onStart();
    }

    Runnable r = new Runnable() {
        @Override
        public void run() {
            vp.setCurrentItem(vp.getCurrentItem() + 1);
            h.postDelayed(this, 3000);
        }
    };

    @Override
    protected void onStop() {
        h.removeCallbacks(r);
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        h.removeCallbacksAndMessages(null);
        super.onDestroy();
    }

    public void selct() {
        //表名 FoodList
        //food_Img 图片
        //food_name 名称
        //food_Price 价格
        //food_Detail 详情
        final ArrayList<FoodList> lxrs = new ArrayList<FoodList>();
        final AVQuery<AVObject> avQuery = new AVQuery<>("FoodList");

        avQuery.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                for (int i = 0; i < list.size(); i++) {
                    FoodList foodList = new FoodList();
                    food_Img = list.get(i).getString("food_Img");
                    food_Name = list.get(i).getString("food_Name");
                    food_Price = list.get(i).getString("food_Price");
                    food_Detail = list.get(i).getString("food_Detail");
                    food_Id = list.get(i).getObjectId();
                    foodList.setFood_Img(food_Img);
                    foodList.setFood_name(food_Name);
                    foodList.setFood_Price(food_Price);
                    foodList.setFood_Detail(food_Detail);
                    foodList.setFood_Id(food_Id);
                    Log.d(food_Img, "asd");
                    Log.d(food_Name, "asd");
                    Log.d(food_Price, "asd");
                    Log.d(food_Detail,"asd");
                    lxrs.add(foodList);
                }
                ListAdapter adapter = new ListAdapter(lxrs, MainActivity.this);
                lv.setAdapter(adapter);
            }
        });

    }

    View.OnClickListener oc = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent it = new Intent(MainActivity.this, zhujiemian_fenlei.class);
            switch (view.getId()) {
                case R.id.ll_item1:
                    it.putExtra("acc", "美食");
                    startActivity(it);
                    break;
                case R.id.ll_item2:
                    it.putExtra("acc", "水果");
                    startActivity(it);
                    break;
                case R.id.ll_item3:
                    it.putExtra("acc", "甜品饮品");
                    startActivity(it);
                    break;
                case R.id.ll_item4:
                    it.putExtra("acc", "速食简吃");
                    startActivity(it);
                    break;
                case R.id.ll_item5:
                    it.putExtra("acc", "休闲零食");
                    startActivity(it);
                    break;
                case R.id.ll_item6:
                    it.putExtra("acc", "烧烤海鲜");
                    startActivity(it);
                    break;
                case R.id.ll_item7:
                    it.putExtra("acc", "包子米粥");
                    startActivity(it);
                    break;
                case R.id.ll_item8:
                    it.putExtra("acc", "无辣不欢");
                    startActivity(it);
                    break;
            }
        }
    };

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        //Intent intent = new Intent(MainActivity.this, DatailActivity.class);
        FoodList foodList = (FoodList) adapterView.getItemAtPosition(i);
        Dialog dialog = new Dialog(MainActivity.this,foodList.getFood_name(),foodList.getFood_Price(),
                foodList.getFood_Img(),foodList.getFood_Detail(),foodList.getFood_Id());
        dialog.show();


    }

    @Override
    public void onRefresh() {// 监听下拉刷新
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(2 * 1000);
                } catch (InterruptedException e) {
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        selct();//获取最新数据
                        refreshLayout.setRefreshing(false);// 终止刷新
                    }
                });
            }
        }).start();

    }


}
